package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.jxj.entity.Bonus;
import com.yang.jxj.entity.Students;
import com.yang.jxj.entity.Teachers;
import com.yang.jxj.service.impl.BonusServiceImpl;
import com.yang.jxj.service.impl.StudentsServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/api/bonus")
public class BonusController {

    @Autowired
    private StudentsServiceImpl stu;
    @Autowired
    private BonusServiceImpl bonu;

    @PostMapping("/save")
    public ResponseEntity<HashMap<String, Object>> save(@RequestBody Bonus bonus, HttpServletRequest request) {
        System.out.println(bonus);
        HashMap<String, Object> map = new HashMap<>();
        Integer userId = Integer.valueOf(String.valueOf(request.getAttribute("userId")));
        bonus.setStudentId(userId);
        bonus.setStatus("待审核");
        boolean b = bonu.saveOrUpdate(bonus);
        if (b) {
            map.put("code", 0);
            map.put("msg", "申请成功");
            return ResponseEntity.ok(map);
        }
        map.put("code", 1);
        map.put("msg", "申请失败");
        return ResponseEntity.ok(map);
    }

    @PostMapping("/del")
    public HashMap<String, Object> del(@RequestBody Bonus bonus) {
        HashMap<String, Object> map = new HashMap<>();
        System.out.println(bonus);
        QueryWrapper<Bonus> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("student_id", bonus.getStudentId());
        objectQueryWrapper.eq("creat_time", bonus.getCreatTime());
        boolean remove = bonu.remove(objectQueryWrapper);
        if (remove) {
            map.put("code", 0);
            map.put("msg", "删除成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "删除失败");
            return map;
        }
    }

    @GetMapping("/botable")   //分页查询
    public IPage<Bonus> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam String studentId,
                                 @RequestParam String status) {
        Page<Bonus> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Bonus> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.like("status", status);
        objectQueryWrapper.orderByAsc("student_id");
        return bonu.page(page, objectQueryWrapper);

    }

    @PostMapping("/return")   //驳回
    public HashMap<String, Object> getReturn(@RequestBody Bonus bonus) {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<Bonus> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", bonus.getStudentId());
        wrapper.eq("creat_time", bonus.getCreatTime());
        Bonus one = bonu.getOne(wrapper);
        one.setStatus("未通过");
        boolean b = bonu.saveOrUpdate(one);
        if (b) {
            map.put("code", 0);
            map.put("msg", "驳回成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "驳回失败");
            return map;
        }
    }


    @GetMapping("/show")   //分页查询加分项
    public IPage<Bonus> findPage1(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String studentId) {
        Page<Bonus> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Bonus> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.orderByAsc("student_id");
        return bonu.page(page, objectQueryWrapper);
    }

    @PostMapping("/saorup")
    public HashMap<String,Object> saorup(@RequestBody Bonus bonus) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = bonu.saveOrUpdate(bonus);
        if(b){
            map.put("code",0);
            map.put("msg","成功");
            return map;
        }else{
            map.put("code",1);
            map.put("msg","失败");
            return map;
        }
    }


}